/**
 * 
 * 游戏牌局的管理者     var foo=(x)=>x+1 等价于 var foo=function(x){return x+1}
 */
import { _decorator, Component, Node, Prefab, instantiate, math } from 'cc';
import { Poker } from './Poker';
import { UIPoker } from './UIPoker';
const { ccclass, property } = _decorator;

@ccclass('GameCtrl')
export class GameCtrl extends Component{
    
    private pokerContainer_:Node=null;
    public pokerPrefab:Prefab=null;

    private pokers:Poker[]=[];

    public Init(pokerContainer:Node,pokerPrefab:Prefab)
    {
        this.pokerPrefab=pokerPrefab;
        this.pokerContainer_=pokerContainer;
    }

    public start()
    {
        console.log("gamectrl");
        //初始化扑克
        for (let point = 1; point <=13; point++) {
           for (let suit = 0; suit < 4; suit++) {
              let poker_=new Poker(point,suit);
              this.pokers.push(poker_);
               
           }
            
        }
        console.log(this.pokers);


        //创建所有扑克牌
        this.pokers.forEach(poker_=>{
            let uiPoker=this.CreateUPocker(poker_);
            this.pokerContainer_.addChild(uiPoker.node);

        })

    }
    
    private CreateUPocker(poker:Poker):UIPoker
    {
          let uiPokerNode=instantiate(this.pokerPrefab);
          let uiPoker:UIPoker=uiPokerNode.getComponent(UIPoker);
          uiPoker.Init(poker);
          uiPoker.node.setPosition(Math.random()*200,Math.random()*200);//或者是CC.randomRangeInt
          return uiPoker;
    }

}
